# -*- coding: utf-8 -*-

#    OriginDB
#    Copyright (C) 2010  Brian Plattenburg <alphastryk@gmail.com>
#    Copyright (C) 2010  Jason Power <power.jg@gmail.com>
#    Copyright (C) 2010  Travis Gockel <travis@gockelhut.com>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import unittest
import sys
import yaml

sys.path.append('../')

from aeConn import dbHandle

from reference import Reference
import referenceData
from testCommon import *

class ReferenceTest (unittest.TestCase):

	def testCreate(self):
		ref = Reference(reference1)
		return
		
	def testEqual(self):
		ref = Reference(reference1)
		ref2 = Reference(reference1)
		self.assertEqual(ref, ref2)
		return
			
	def testNotEqual(self):
		ref = Reference(reference1)
		ref2 = Reference(reference2)
		self.assertNotEqual(ref, ref2)
		return
			
	def testGetType(self):
		ref = Reference(reference1)
		self.assertEqual(ref.getType(), ref.__class__.__name__)
		return
	
class ReferenceYamlTest (unittest.TestCase):
	
	def testLoadStringYaml(self):
		for i in range(len(referenceData.references)):
			yml = referenceData.references[i]
			ref = Reference()
			ref._loadYAML(yml)
			goodYaml = yaml.load(yml)
			authors = ref.author_name.split("; ")
			self.assertEqual(authors, goodYaml['authors'])
			self.assertEqual(ref.year, goodYaml['year'])
			self.assertEqual(ref.title, goodYaml['title'])
		return
	
	def testDumpYaml(self):
		for i in range(len(referenceData.references)):
			yml = referenceData.references[i]
			ref = Reference()
			ref._loadYAML(yml)
			refDump = ref._dumpYAML()
			goodDump = yaml.dump(ref._things)
			self.assertEqual(refDump, goodDump)
		return
		
	
class ReferenceDBTest (unittest.TestCase):
	
	def setUp(self):
		setupDB()
		return

	def testInsertGet(self):
		for i in referenceData.references:
			resp = dbHandle.postAction("insert", i, {'type':"Reference"})
			d = yaml.load(resp)
			self.assert_("key" in d.keys())
			resp = dbHandle.getAction("get", {'type':"Reference", 'key':d['key']})
			dataGot = yaml.load(resp)
			data = yaml.load(i)
			for key in data:
				self.assertEqual(data[key], dataGot[key], "for %s had %s got %s" % (key, data[key], dataGot[key]))

	def testUpdateTitle(self):
		keys = []
		for i in referenceData.references:
			resp = dbHandle.postAction("insert", i, {'type':"Reference"})
			data = yaml.load(resp)
			keys.append(data['key'])
		for i in keys:
			body = yaml.dump({"title":"This is a test title"})
			dbHandle.postAction("update", body, {'type':'Reference', 'key':i})
		for i in keys:
			resp = dbHandle.getAction("get", {'type':"Reference", 'key':i})
			dataGot = yaml.load(resp)
			self.assertEqual(dataGot['title'], "This is a test title")

	def testUpdateAuthors(self):
		keys = []
		for i in referenceData.references:
			resp = dbHandle.postAction("insert", i, {'type':"Reference"})
			data = yaml.load(resp)
			keys.append(data['key'])
		for i in keys:
			body = yaml.dump({"authors":["one", "two", "three"]})
			dbHandle.postAction("update", body, {'type':'Reference', 'key':i})
		for i in keys:
			resp = dbHandle.getAction("get", {'type':"Reference", 'key':i})
			dataGot = yaml.load(resp)
			self.assertEqual(dataGot['authors'], ["one", "two", "three"])

	def testUpdateSource(self):
		keys = []
		for i in referenceData.references:
			resp = dbHandle.postAction("insert", i, {'type':"Reference"})
			data = yaml.load(resp)
			keys.append(data['key'])
		for i in keys:
			body = yaml.dump({"source":"TEST"})
			dbHandle.postAction("update", body, {'type':'Reference', 'key':i})
		for i in keys:
			resp = dbHandle.getAction("get", {'type':"Reference", 'key':i})
			dataGot = yaml.load(resp)
			self.assertEqual(dataGot['source'], "TEST")

	def testUpdateAbstract(self):
		keys = []
		for i in referenceData.references:
			resp = dbHandle.postAction("insert", i, {'type':"Reference"})
			data = yaml.load(resp)
			keys.append(data['key'])
		for i in keys:
			body = yaml.dump({"abstract":"asdf;lkjasd;fl\n\n;alsdf;alfjd\n\n;lasdf;las;ldf"})
			dbHandle.postAction("update", body, {'type':'Reference', 'key':i})
		for i in keys:
			resp = dbHandle.getAction("get", {'type':"Reference", 'key':i})
			dataGot = yaml.load(resp)
			self.assertEqual(dataGot['abstract'], "asdf;lkjasd;fl\n\n;alsdf;alfjd\n\n;lasdf;las;ldf")

	def testUpdateYear(self):
		keys = []
		for i in referenceData.references:
			resp = dbHandle.postAction("insert", i, {'type':"Reference"})
			data = yaml.load(resp)
			keys.append(data['key'])
		for i in keys:
			body = yaml.dump({"year":2018})
			dbHandle.postAction("update", body, {'type':'Reference', 'key':i})
		for i in keys:
			resp = dbHandle.getAction("get", {'type':"Reference", 'key':i})
			dataGot = yaml.load(resp)
			self.assertEqual(dataGot['year'], 2018)

	def testUpdateVolume(self):
		keys = []
		for i in referenceData.references:
			resp = dbHandle.postAction("insert", i, {'type':"Reference"})
			data = yaml.load(resp)
			keys.append(data['key'])
		for i in keys:
			body = yaml.dump({"volume":2018})
			dbHandle.postAction("update", body, {'type':'Reference', 'key':i})
		for i in keys:
			resp = dbHandle.getAction("get", {'type':"Reference", 'key':i})
			dataGot = yaml.load(resp)
			self.assertEqual(dataGot['volume'], 2018)

	def testUpdateIssue(self):
		keys = []
		for i in referenceData.references:
			resp = dbHandle.postAction("insert", i, {'type':"Reference"})
			data = yaml.load(resp)
			keys.append(data['key'])
		for i in keys:
			body = yaml.dump({"issue":2018})
			dbHandle.postAction("update", body, {'type':'Reference', 'key':i})
		for i in keys:
			resp = dbHandle.getAction("get", {'type':"Reference", 'key':i})
			dataGot = yaml.load(resp)
			self.assertEqual(dataGot['issue'], 2018)

	def testUpdatePageStart(self):
		keys = []
		for i in referenceData.references:
			resp = dbHandle.postAction("insert", i, {'type':"Reference"})
			data = yaml.load(resp)
			keys.append(data['key'])
		for i in keys:
			body = yaml.dump({"pageStart":2018})
			dbHandle.postAction("update", body, {'type':'Reference', 'key':i})
		for i in keys:
			resp = dbHandle.getAction("get", {'type':"Reference", 'key':i})
			dataGot = yaml.load(resp)
			self.assertEqual(dataGot['pageStart'], 2018)

	def testUpdatePageEnd(self):
		keys = []
		for i in referenceData.references:
			resp = dbHandle.postAction("insert", i, {'type':"Reference"})
			data = yaml.load(resp)
			keys.append(data['key'])
		for i in keys:
			body = yaml.dump({"pageEnd":2018})
			dbHandle.postAction("update", body, {'type':'Reference', 'key':i})
		for i in keys:
			resp = dbHandle.getAction("get", {'type':"Reference", 'key':i})
			dataGot = yaml.load(resp)
			self.assertEqual(dataGot['pageEnd'], 2018)

	def testSearchYear(self):
		keys = []
		years = []
		for i in referenceData.references:
			data = yaml.load(i)
			#	skip items without year
			if not data['year']:
				continue
			
			years.append(data['year'])
			resp = dbHandle.postAction("insert", i, {'type':"Reference"})
			data = yaml.load(resp)
			keys.append(data['key'])
		
		for year in years:
			body = yaml.dump({'year':year})
			response = dbHandle.postAction("search",body, {'type': "Reference"})
			keysGot = yaml.load(response)
			for idx in range(len(years)):
				if (years[idx] == year):
					self.assert_(keys[idx] in keysGot)

	def testSearchAuthor(self):
		pass

	def testSearchTitle(self):
		pass

	def testSearchSource(self):
		pass

	def tearDown(self):
		clearDB()
		return
	
	
if __name__ == "__main__":
	unittest.main()   